package mr.ali.webvideo.service.impl;

import mr.ali.webvideo.model.CollectionUserMony;
import mr.ali.webvideo.model.CollectionUserMonyIncome;
import mr.ali.webvideo.model.OpenId;
import mr.ali.webvideo.mapper.OpenIdMapper;
import mr.ali.webvideo.service.ICollectionUserMonyIncomeService;
import mr.ali.webvideo.service.ICollectionUserMonyService;
import mr.ali.webvideo.service.IOpenIdService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Mr-ali
 * @since 2025-08-16
 */
@Service
public class OpenIdServiceImpl extends ServiceImpl<OpenIdMapper, OpenId> implements IOpenIdService {

    @Resource
    private OpenIdMapper openIdMapper;

    @Override
    public void InsertOpenId(OpenId openId) {
        openIdMapper.insert(openId);
    }

    @Override
    public OpenId getOpenId(String openId) {
        return openIdMapper.getOpenId(openId);
    }

    @Override
    public OpenId getById(Integer id) {
        return openIdMapper.selectById(id);
    }

    /**
     * 非会员身份变成会员
     */
    @Override
    public OpenId updateOpenId(Integer id, Long statusCreate) {
        Long index = openIdMapper.selectById(id).getStatusCreate();
        if (index < System.currentTimeMillis()) {
            openIdMapper.updateStatus(id, 1, statusCreate);
            return openIdMapper.selectById(id);
        } else {
            statusCreate = index + (statusCreate - System.currentTimeMillis());
            openIdMapper.updateStatus(id, 1, statusCreate);
            return openIdMapper.selectById(id);
        }
    }


    /**
     * 会员身份变成非会员
     */
    @Override
    public OpenId deleteStatus(Integer id) {
        openIdMapper.deleteStatus(id, 0);
        return openIdMapper.selectById(id);
    }


    @Resource
    private ICollectionUserMonyService collectionUserMonyService;
    @Resource
    private ICollectionUserMonyIncomeService collectionUserMonyIncomeService;

    /**
     * 更新用户推广费用
     */
    @Override
    public void updateNotMoney(Integer id, Double money, Boolean close, String channel) {
        Double index = openIdMapper.selectById(id).getNotMoney();
        Double yesIndex = openIdMapper.selectById(id).getYesMoney();
        if (close) {
            money = index + money;
            openIdMapper.updateNotMoney(id, money);
            CollectionUserMonyIncome income = new CollectionUserMonyIncome();
            income.setUserId(id)
                    .setMoney(money)
                    .setGmtCreate(System.currentTimeMillis())
                    .setChannel(money > 0.5 ? "引导购买会员奖励" : "引导新用户奖励");
            collectionUserMonyIncomeService.insertCollectionUserMonyIncome(income);
        }
        if (!close) {
            CollectionUserMony income = new CollectionUserMony();
            income.setMoney(money)
                    .setUserId(id)
                    .setChannel(channel)
                    .setGmtCreate(System.currentTimeMillis());
            collectionUserMonyService.insertCollectionUserMony(income);
            Double yesMoney = yesIndex + money;
            openIdMapper.updateYesMoney(id, yesMoney);
            money = index - money;
            openIdMapper.updateNotMoney(id, money);
        }

    }

}
